<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/4/17 0017
 * Time: 10:33
 */
defined('BASEPATH') OR exit('No direct script access allowed');
include_once('./simple_html_dom.php');

class Jiankang_arc extends CI_Controller
{

    public function info(){
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        // 从list拿出来一个，如果执行不成功，写到另外的list里
        $urlid = $redis->lpop('jiankang_arc_list');
        //$urlid = 1;
        if(!$urlid){
            echo "nodata";
            exit;
        }
        $url_info = $this->db->query("select * from res_article_res where id=".$urlid)->row_array();
        $disease_id = $url_info['disease_id'];

        $disease_info = $this->db->query("select * from ai_spider_39_disease where did=".$disease_id)->row_array();
        $disease_name = $disease_info['disease_name'];

        //echo "采集开始:".time()."-";
        $detail_url = "https://www.99.com.cn/wenzhang/".$url_info['cid'].".html";
        //echo $detail_url;
        $content = geturl($detail_url);
        $html = str_get_html($content);
        // 处理对应字段
        $department_1 = $disease_info['department_1'];
        $department_2 = $disease_info['department_2'];
        $disease_name = $disease_info['disease_name'];
        $video_name = trim($html->find("h1",0)->innertext);
        $video_content = trim($html->find("div[class=artle-cont]",0)->innertext);
        $doctor_name = $html->find("dd",0)->find('b',0)->find('a',0)->innertext;
        $doctor_title = $html->find("dd",0)->find('b',0)->find('span',0)->innertext;
        $hospital = $html->find("dd",0)->find('p',0)->find('span',0)->innertext;
        $department = $html->find("dd",0)->find('p',0)->find('font',0)->innertext;
        $pubdate = trim($html->find("div[class=atcle-info]",0)->find('span',0)->innertext);
        $click_num = trim($html->find("div[class=atcle-info]",0)->find('span',1)->innertext);
        $click_num = str_replace("阅读 : ","",$click_num);

        $sql = "INSERT ignore INTO ai_spider_39_disease_article(department_1,department_2,disease,article_name,article_content,pubdate,click_num,author_name,author_title,author_hospital,author_department,vid) VALUES ('".$department_1."','".$department_2."','".$disease_name."','".$video_name."','".$video_content."','".$pubdate."','".$click_num."','".$doctor_name."','".$doctor_title."','".$hospital."','".$department."','".$url_info['cid']."')";
        //echo $sql;exit;
        $this->db->query($sql);
        echo 1;
        //$ = $html->find("ul[id=videoList] li");
    }

    public function cid_push(){
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        $ids = $this->db->query("select id from res_article_res")->result_array();
        foreach($ids as $id){
            $redis->rPush('jiankang_arc_list',$id['id']);
        }

    }
}